/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.javatanf2013.web.examples.jsps;

import com.epam.javatanf2013.services.CustomerService;
import com.epam.javatanf2013.services.ServiceException;
import com.epam.javatanf2013.services.bo.CustomerBo;
import com.epam.javatanf2013.web.examples.servlets.CustomerEditServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

/**
 *
 * @author Istvan_Monori
 */
public class CustomerJSPEditServlet extends HttpServlet {

    @Autowired
    private CustomerService customerService;

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
                config.getServletContext());
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger log = Logger.getLogger("log");
        String sId = request.getParameter("id");
        CustomerBo co = null;
        try {
            if (sId != null) {
                co = getCustomerService().getCustomerById(Integer.valueOf(sId));
            }
            request.setAttribute("customerBean", co);
            this.getServletContext().getRequestDispatcher("/customer/customerEdit.jsp").forward(request, response);
        } catch (ServiceException ex) {
            log.log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger log = Logger.getLogger(CustomerEditServlet.class.getName());
        boolean bError = false;
        try {
            if (request.getParameter("customer_firstname")!= null && "".equals(request.getParameter("customer_firstname"))) {
                 request.setAttribute("error.customer.fistname", "Required.");
                 bError = true;
            }
            if (request.getParameter("customer_lastname")!= null && "".equals(request.getParameter("customer_lastname"))) {
                 request.setAttribute("error.customer.lastname", "Required.");
                 bError = true;
            }
            if (request.getParameter("customer_id")== null) {
                 request.setAttribute("error.customer.id", "Required.");
                 bError = true;
            }
            if (!bError) {
                String sId = request.getParameter("customer_id");
                CustomerBo co = null;
                if (!"".equals(sId)) {
                    Integer id = Integer.valueOf(sId);
                    co = getCustomerService().getCustomerById(id);
                } else {
                    co = new CustomerBo();
                }
                /**
                 * @TODO implement missing methods
                 */
                //getCustomerService().saveCustomer(co);
                
                response.sendRedirect(request.getContextPath() + "/CustomerJSP");
                return;
            }
            processRequest(request, response);
        }   catch (ServiceException ex) {
            log.log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
